/*
 * 查房列表
 * @author 醉雪
 * @time 2018/10/17
 * @since 1.0.0
 * */
// React基础组件
import React from 'react'
// dva 连接组件 - 连接route和model
import { connect } from 'dva'
// 引入antd的组件
import {Form, Select, Input, DatePicker, Button, Popconfirm, Modal} from 'antd'
import moment from 'moment';
const { RangePicker } = DatePicker;
const dateFormat = 'YYYY/MM/DD';
//导入路由组件，用于跳转等
import { Link, hashHistory } from 'dva/router'
// 导入项目全局配置文件
import config from 'config/config'

// 引入model取值工具方法
import { mapStateToProps } from 'utils/view_utils'
// 当前组件样式
import styles from './consultantAdminStyle.less'
//引用listPage
import ListPage from 'widget/PageTmpl/ListPage'
import cx from 'classnames'

//权限管理
import FlagBtn from 'widget/FlagBtn/FlagView'

const Option = Select.Option;
/**
 * description 路由视图及事件处理
 * dispatch 分发到models中的effects对应事件名
 * model 用于获取models中state定义的数据
 * form 表单对象
 */

const infoListAdminView = function ({ dispatch, model, form }) {
    // 表单的子函数
    const { resetFields, getFieldDecorator, validateFields, setFieldsValue } = form
    // 取出model对象，命名空间
    const { modelObj, namespace } = model
  //console.log('modelObj',modelObj)
    // modelObj.xxx获取model中状态机的值
    let { showModal, score } = modelObj

    const FormItem = Form.Item;
    // 资讯详情页跳转
    function detailSkip(type, id = '', productId, lock) {
        hashHistory.push({
            pathname: '/home/realEstateConsultantAdmin/editConsultant',
            query: { type, id, productId ,lock}
        });
    }
    // 弹出调整修改分数对话框
    function modifyPoint(param){
        dispatch({
            type: `${namespace}/updateStore`,
            payload: { showModal:true }
        });

        dispatch({
            type: `${namespace}/updateStore`,
            payload: { score: param}
        })
    }
    //对话框点击ok
    function handleOk (){
        dispatch({
            type: `${namespace}/updateStore`,
            payload: { showModal:false }
        });
    }
    //对话框点击取消
    function handleCancel(){
        dispatch({
            type: `${namespace}/updateStore`,
            payload: { showModal:false }
        });
    }

    let pageProps = {
        // ---------- 界面部分 ----------
        "ui": {
            // 页面接口地址
            "api_url": "admin/propertyConsultant/queryList",
            // 接口方式
            "method": "POST",
            "app": "",
            // 接口参数
            "params": "",
            // 模拟数据 - 用于开发需要，若填充了上面的api_url之后，请将此项设置为null
            // "mockData": mockData,
            "mockData": null,
            // 查询栏
            "search_bar": {
                "fields": [
                    {
                        en_name: 'name',
                        zh_name: '置业顾问姓名',
                        elem_type: 'Input',
                        elem_valid_type: 'string',
                    },
                    // {
                    //     en_name: 'uid',
                    //     zh_name: '用户账号',
                    //     elem_type: 'Input',
                    //     elem_valid_type: 'string',
                    // },
                    {
                        en_name: 'mobile',
                        zh_name: '手机号码',
                        elem_type: 'Input',
                        elem_valid_type: 'string',
                    },
                    {
                        en_name: 'productName',
                        zh_name: '项目名称',
                        elem_type: 'Input',
                        elem_valid_type: 'string',
                    },
                    {
                        en_name: 'minPoint',
                        zh_name: '评价平均分',
                        elem_type: 'Select',
                        elem_valid_type: 'string',
                        cmpt_items: [
                            {
                                value: '0',
                                label: '0分'
                            },
                            {
                                value: '1',
                                label: '1分'
                            },
                            {
                                value: '2',
                                label: '2分'
                            },
                            {
                                value: '3',
                                label: '3分'
                            },
                            {
                                value: '4',
                                label: '4分'
                            }
                        ]
                    },
                    {
                        en_name: 'maxPoint',
                        zh_name: '至',
                        elem_type: 'Select',
                        elem_valid_type: 'string',
                        cmpt_items: [
                            {
                                value: '1',
                                label: '1分'
                            },
                            {
                                value: '2',
                                label: '2分'
                            },
                            {
                                value: '3',
                                label: '3分'
                            },
                            {
                                value: '4',
                                label: '4分'
                            },
                            {
                                value: '5',
                                label: '5分'
                            }
                        ]
                    },

                   

                ],
                searchHandler: () => { // 自定义搜索按钮回调函数，点击搜索时触发
                    console.log('search click',modelObj.biz)
                    modelObj.biz.queryForm.productId = null
                    dispatch({
                        type: `${namespace}/updateStore`,
                        payload: modelObj.biz
                    })

                },
                resetHandler: () => { // 自定义重置按钮回调函数，点击重置时触发
                    console.log('reset click')
                },
                searchCallback: params => { // 自定义搜索完成后的后调函数, 搜索完成后触发, 并将本次搜索的参数传入
                    console.log('search finish', params)
                },
                pasteTargetList: [  // 列表组件中将输入框的en_name作为默认id(也可以通过id另外设置)
                    'carName',  // 将需要粘贴事件拦截的输入框id以数组的形式传入, 该参数对应PasteFromClipboard组件的targetList参数
                ]
            },
            action_bar: [
                // {
                //   func_name: "onAdd",
                //   label: "导出模板",
                //   type: "",
                //   icon: "",
                //   onClick: (e) => {
                //     dispatch({
                //       type: `${namespace}/updateStore`,
                //       payload: { actionType: "add", showAddVisible: true }
                //     })
                //   }
                // },
                // {
                //   func_name: "onAdd",
                //   label: "导入",
                //   type: "",
                //   icon: "",
                //   onClick: (e) => {
                //     dispatch({
                //       type: `${namespace}/updateStore`,
                //       payload: { actionType: "add", showAddVisible: true }
                //     })
                //   }
                // },
                {
                    func_name: "onAdd",
                    label: "新增顾问",
                    type: "primary",
                    icon: "plus",
                    btnId:'80101',
                    onClick: (e) => {
                        detailSkip('add');
                    }
                }
            ],
            // 数据表格
            "table":
                {
                    "status_params": "status=0",
                    "status_text": "全部",
                    // 表头字段列表
                    "scroll": { x: 1500 },
                    "fields": [
                        // {
                        //   "en_name": "key",
                        //   "zh_name": "置业顾问编号",
                        //   render: (a, item, index, d, e, f) => {
                        //     return (
                        //       <span>{index + 1}</span>
                        //     )
                        //   },
                        // },
                        // {
                        //     "en_name": "outerAccount",
                        //     "zh_name": "用户账号",
                        // },
                        {
                            "en_name": "counselorId",
                            "zh_name": "置业顾问编号",
                        },
                        {
                            "en_name": "productName",
                            "zh_name": "项目名称",
                        },
                        {
                            "en_name": "cityName",
                            "zh_name": "所属城市",
                            width:150,
                            render: (text, item, index) => {
                                return `${item.provinceName||''}${item.provinceName&&item.cityName&&' - '||''}${item.cityName||''}`
                            }
                        },
                        {
                            "en_name": "name",
                            "zh_name": "置业顾问姓名",
                            width:80,
                        },
                        // {
                        //   "en_name": "idCardNo",
                        //   "zh_name": "身份证号码",
                        // },
                        {
                            "en_name": "sex",
                            "zh_name": "性别",
                            width:60,
                            render: (a, item, index, d, e, f) => {
                                if(!(item.sex == undefined || item.sex == null)){
                                    return item.sex === 1 ? '男' : '女'
                                }
                            }
                        },
                        {
                            "en_name": "mobile",
                            "zh_name": "手机号码",
                            width:130,
                        },

                        // {
                        //     "en_name": "evaluateAvgPoints",
                        //     "zh_name": "平均分",
                        //     render: (item, index, d, e, f) => {
                        //       return item && Number(item).toFixed(1)||''
                        //     }
                        // },
                        // {
                        //     "en_name": "displayAvgPoints",
                        //     "zh_name": "显示分",
                        //     render: (item, index, d, e, f) => {
                        //       return item && Number(item).toFixed(1)||''
                        //     }
                        // },
                        {
                            "en_name": "creator",
                            "zh_name": "创建人",
                        },
                        {
                            "en_name": "createTime",
                            "zh_name": "创建时间",
                            width:180,
                        },
                        {
                            "en_name": "edit",
                            "zh_name": "操作",
                            width:160,
                            render: (a, item, index, d, e, f) => {
                                // item.scorr
                                return (
                                    <span>
                                        <FlagBtn className={styles.wid} node={<a onClick={() => detailSkip('edit', item.counselorId, item.productId)}>编辑<span className="ant-divider" /></a>} btnId={'80102'}></FlagBtn>
                                        {/* <a onClick={() => detailSkip('edit', item.counselorId, item.productId)}>编辑</a> */}
                                        <FlagBtn className={styles.wid} node={<a onClick={() => detailSkip('check',item.counselorId, item.productId, true)}>查看<span className="ant-divider" /></a>} btnId={'80103'}> </FlagBtn>
                                        {/* <a onClick={() => detailSkip('edit',item.counselorId, item.productId, true)}>查看</a> */}
                                        {/*<a onClick={() => modifyPoint(3.8)}>修改分数</a><span className="ant-divider" />*/}
                                        <FlagBtn className={styles.wid} node={<Popconfirm Collapse title='确定要删除吗？' okText='确定' cancelText='取消' onConfirm={() => {
                                            console.log('warn');
                                            dispatch({ type: `${namespace}/deleteConsultant`, payload: { propertyConsultantId: item.counselorId,productId:item.productId, cb: () => { fresh() } } });
                                        }}>
                                            <a >删除</a>
                                        </Popconfirm> } btnId={'80104'}>
                                        </FlagBtn>
                                        {/* <Popconfirm Collapse title='确定要删除吗？' okText='确定' cancelText='取消' onConfirm={() => {
                                            console.log('warn');
                                            dispatch({ type: `${namespace}/deleteConsultant`, payload: { propertyConsultantId: item.counselorId,productId:item.productId, cb: () => { fresh() } } });
                                        }}>
                    <a >删除</a>
                  </Popconfirm> */}
                </span>
                                )
                            },
                        },
                    ]
                },
        },
    }
    if (!!modelObj.biz) {
        pageProps.biz = modelObj.biz
        pageProps.biz.syncBackCbf = (biz) => {
            dispatch({
                type: `${namespace}/setFormVal`,
                payload: biz
            })
        }
    }
    // 表单刷新
    function fresh() {
        modelObj.biz.refreshListDatas(true)
    }
    return (
        <div className={cx('pageLayout')}>
            <ListPage pageProps={pageProps} />
        </div>

    )
}

// 连接视图(view)和模型(model)
export default connect(mapStateToProps)(Form.create()(infoListAdminView))
